package com.example.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.graduation.entity.*;
import com.example.graduation.exception.MyException;
import com.example.graduation.mapper.*;
import com.example.graduation.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.graduation.util.JwtUtil;
import com.example.graduation.vo.OrderDetailVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xyc
 * @since 2022-02-11
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Torder> implements OrderService {

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 查询用户订单
     *
     * @param token
     * @return {@link List< OrderDetailVo>}
     * @author xieyucan
     * CreateDate 2022/3/2 17:02
     */
    @Override
    public List<OrderDetailVo> selectUserOrder(String token) {
//        try {
//            List<OrderDetailVo> list=new ArrayList<>();
//
//            OrderDetailVo orderDetailVo = new OrderDetailVo();
//            //通过token获取用户id
//            int userId = jwtUtil.getUserIdByToken(token);
//            //先查询出用户的订单大体信息
//            List<OrderVo> orderVos = orderMapper.selectOrderList(userId);
//            //通过大体信息，查询用户的订单具体内容
//            for(OrderVo orderVo:orderVos)
//            {
//                List<Goods> goods = goodsMapper.selectGoodsByOrderId(orderVo.getOrderId());
//                orderDetailVo.setOrderVo(orderVo);
//                orderDetailVo.setList(goods);
//                list.add(orderDetailVo);
//            }
//            return list;
//        }catch (Exception e){
//            throw new MyException(e.getMessage());
//        }
        return null;
    }


    /**
     * 查询商家的订单信息
     *
     * @param token
     * @return
     */
    public List<Torder> findShopOrder(String token) {
        int userId = jwtUtil.getUserIdByToken(token);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Shop shop = shopMapper.selectOne(wrapper);
        QueryWrapper<Torder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", shop.getShopId());
        List<Torder> torders = orderMapper.selectList(queryWrapper);
        for (Torder torder : torders) {
            User user = userMapper.selectById(torder.getUserId());
            torder.setUser(user);
            QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
            shoppingCartQueryWrapper.eq("order_id", torder.getOrderId());
            List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCartQueryWrapper);
            List<Goods> list = new ArrayList<>();
            for (ShoppingCart shoppingCart : shoppingCarts) {
                Goods goods = goodsMapper.selectById(shoppingCart.getGoodsId());
                list.add(goods);
            }
            torder.setList(list);
        }
        return torders;
    }


    /**
     * 根据id查询订单信息
     *
     * @param orderId
     * @return
     */
    public Torder findOrderById(Long orderId) {
        try {
            QueryWrapper<Torder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", orderId);
            Torder torder = orderMapper.selectOne(wrapper);
            User user = userMapper.selectById(torder.getUserId());
            torder.setUser(user);
            QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", orderId);
            List<ShoppingCart> list = shoppingCartMapper.selectList(queryWrapper);
            ArrayList<Goods> arrayList = new ArrayList<>();
            for (ShoppingCart cart : list) {
                Goods goods = goodsMapper.selectById(cart.getGoodsId());
                arrayList.add(goods);
            }
            torder.setList(arrayList);
            return torder;
        } catch (Exception e) {
            throw new MyException(e.getMessage());
        }

    }


    /**
     * 1工具订单号查询用户信息
     *
     * @param orderId
     * @return
     */
    public User findUserById(Long orderId) {
        try {
            QueryWrapper<Torder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", orderId);
            Torder torder = orderMapper.selectOne(wrapper);
            Integer userId = torder.getUserId();
            QueryWrapper<User> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("id", userId);
            User user = userMapper.selectOne(wrapper1);
            return user;
        } catch (Exception e) {
            throw new MyException(e.getMessage());
        }
    }

}
